﻿@{
    var KeyId = Request.QueryString["ID"];
}
@model List<Sys_Function>
    <div class="page-content animated fadeInDown">
        <div class="container-fluid pb-100">
            <div class="row" id="form">
                <!--检索面板-->
                <div class="col-sm-6">
                    <h4 class="example-title">菜单编号</h4>
                    <input class="form-control" type="text" data-bind="value:Menu_Num" placeholder="A 或者 A-100">
                </div>

                <div class="col-sm-6">
                    <h4 class="example-title">菜单名称</h4>
                    <input class="form-control" type="text" data-bind="value:Menu_Name" placeholder="请输入菜单名称">
                </div>

                <div class="col-sm-6">
                    <h4 class="example-title">菜单Href</h4>
                    <input class="form-control" type="text" data-bind="value:Menu_Url" placeholder="请输入菜单Href 如：/Admin/User/Index">
                </div>

                <div class="col-sm-6">
                    <h4 class="example-title">菜单图标</h4>
                    <input class="form-control" type="text" data-bind="value:Menu_Icon" placeholder="请输入菜单图标 如：fa fa-file">
                </div>

                <div class="col-sm-6">
                    <h4 class="example-title">父级菜单</h4>
                    <input class="form-control" type="text" data-bind="value:pname" disabled="disabled">
                </div>

                <div class="col-sm-6">
                    <h4 class="example-title">是否显示</h4>
                    <select class="form-control" data-bind="value:Menu_IsShow">
                        <option value="">==是否显示 该菜单==</option>
                        <option value="1">是</option>
                        <option value="2">否</option>
                    </select>
                </div>

                <div class="col-sm-12">
                    <h4 class="example-title">菜单权限</h4>
                    <div class="row">
                        @foreach (var item in Model)
                        {
                            <div class="col-sm-3">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="Function_ID" value="@item.Function_ID"> @item.Function_Name
                                    </label>
                                </div>
                            </div>
                        }
                    </div>
                </div>


            </div>

        </div>
    </div>
    <!--按钮-->
    <div class="my-ButtonBar text-right">
        <button type="button" class="btn btn-success" data-bind="visible: !App.IsAdd()" onclick="adminForm.resetUrl(null)">新增</button>
        <button class="btn btn-primary" onclick="App.Save()">提交</button>
        <button class="btn btn-danger" onclick="admin.layer.close(admin.getLayerIframeIndex());">关闭</button>
    </div>
    @section css{
        <link href="~/Resource/Admin/lib/icheck/skins/all.css" rel="stylesheet" />
    }
    @section js{
        <script src="~/Resource/Admin/lib/Ko/Knockout-3.4.2.debug.js"></script>
        <script src="~/Resource/Admin/js/admin-form.js"></script>
        <script src="~/Resource/Admin/lib/icheck/icheck.min.js"></script>
        <script type="text/javascript">
        var KeyId = '@KeyId';
        var pId = admin.getQueryString("pId");
        model = new vModel();
        $(function () {
            ko.applyBindings(model);//注册vModel
            App.Load();
        });

        var App = {
            IsAdd: function () {
                return !KeyId;
            },
            Load: function () {
                $('input[type=checkbox]').iCheck({
                    checkboxClass: 'icheckbox_flat-green',
                    radioClass: 'iradio_flat-green'
                });

                adminForm.load({
                    KeyId: KeyId,
                    url: "@Url.Action("LoadForm")",
                    callBack: function (r) {
                        for (var i = 0; i < r.Menu_Power.length; i++) {
                            $("input[value=" + r.Menu_Power[i] + "]").iCheck('check');
                        }

                        if (pId) {
                            admin.ajax({
                                url: "@Url.Action("Find")",
                                data: { ID: pId },
                                success: function (r) {
                                    if (App.IsAdd()) {
                                        model.pname(r.Menu_Name);
                                        model.Menu_ParentID(pId);
                                    }
                                }
                            });
                        } else {
                            model.pname("一级菜单");
                        }

                        if (App.IsAdd()) {
                            model.Menu_IsShow(1);
                        }


                    }
                });
            },
            Save: function () {
                //组装 checkbox
                var json = [];
                $.each($("input[name=Function_ID]"), function (i, e) {
                    if ($(this).prop("checked")) {
                        json.push({
                            Function_ID: $(this).val()
                        });
                    }
                });
                model._Sys_Function_List(JSON.stringify(json));

                adminForm.Save({
                    url: "@Url.Action("Save")",
                    callBack: function (r) { }
                });
            },
            FindBack: {
                CallBack: function (row, tag, dom) {
                    //if (tag == "Role") {
                    //    model.Role_ID(row ? row.Role_ID : "");
                    //    model.Role_Name(row ? row.Role_Name : "");
                    //}
                }
            }
        };

        function vModel() {
            @(UI.CreateKOViewModel(new Sys_Menu(), "pname", "_Sys_Function_List"))
        }
        </script>
    }
